$(function () {
    //jq start
    authorities = $.session.get('authorities');
    console.log(authorities);

    //ajax start
    $.ajax({
        url: baseURL+'/admin/api/btpapers/searchBtPapers',
        data: {
            state: -1,
            pageNum: 1,
            pageSize: 10,
        },
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        success: function (c) {
            add_data(c)
        }
    })
//ajax end
//jQ start 重复调用模板
    function add_data(e) {
        let data_1st = e.content
        for (let i = 0; i < e.total; i++) {
            $("#data-list").append(
                '<tr><td id="nick_name">' + data_1st[i].title + '</td><td id="name">' + data_1st[i].grade + '</td><td id="account">' + data_1st[i].subject + '</td><td id="wx_openId_parent">' + data_1st[i].state + '</td>' +
                '<td><a href="">查看 </a><a href=""> 编辑</a></td></tr>'
            )
        }
    }


    //默认显示数据开始位置
    let pager_num = 0
    let pager_num_ceil = 1
    $("#pager_input").val(1)
    //获取数据总数
    $.ajax({
        url: baseURL+'/admin/api/btpapers/searchBtPapers',
        data: {
            state: -1,
            pageNum: 1,
            pageSize: 1,
        },
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        success: function (e) {
            $("#data_number").text(e.total)
            pager_num_offset = Math.ceil(e.total / 10)
            pager_offset = e.total
        }
    })
//    下一页按钮点击事件
    $("#rarr").click(function () {
        if (pager_num < pager_offset - 10) {
            pager_num = pager_num + 10
            $("#pager_input").val(Math.ceil($("#pager_input").val()) + 1)
            $("#data-list").empty();
            $.ajax({
                url: baseURL+'/admin/api/btpapers/searchBtPapers',
                data: {
                    state: -1,
                    pageNum: pager_num,
                    pageSize: 10,
                },
                type: 'post',
                headers: {'authorities': authorities},
                success: function (c) {
                    add_data(c)
                }
            })
        } else {
            alert("这是最后一页了")
        }

    })
//    上一页按钮点击事件
    $("#larr").click(function () {
        if (pager_num > 0) {
            pager_num = pager_num - 10
            $("#pager_input").val(Math.ceil($("#pager_input").val()) - 1)
            $("#data-list").empty();
            $.ajax({
                url: baseURL+'/admin/api/btpapers/searchBtPapers',
                data: {
                    state: -1,
                    offset: pager_num,
                    pageSize: 10,
                },
                type: 'post',
                headers: {'authorities': authorities},
                success: function (c) {
                    add_data(c)
                }
            })
        }

    })
//    页码失焦时根据页码跳转
    $("#pager_input").blur(function () {
        let rr = $("#pager_input").val()
        if (rr > 0) {
            if (rr <= pager_num_offset) {
                let rrn = (rr * 10) - 10
                pager_num = rrn
                $("#data-list").empty();
                $.ajax({
                    url: baseURL+'/admin/api/btpapers/searchBtPapers',
                    data: {
                        state: -1,
                        pageNum: pager_num + 1,
                        pageSize: 10,
                    },
                    type: 'post',
                    headers: {'authorities': authorities},
                    success: function (c) {
                        add_data(c)
                    }
                })
            } else if (rr > pager_num_offset) {
                alert("没有这么多页")

            }
        } else if (rr < 0) {
            alert("请输入大于0的数")
        }
    });
//jq end
})
//jQ start
$(function () {
    //ajax start 生成学科列表
    $.ajax({
        url: baseURL +'/auth/api/dic/searchDic',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            dicgroup: '科目',
        },
        success: function (e) {

            //for i 循环 start
            for (let i = 0; i < e.content.length; i++) {
                let v_dic = e.content[i].dickey
                let mark_dicv = e.content[i].dicvalue
                $("#subject").prepend(
                    '<option value=' + v_dic + ' mark=' + mark_dicv + ' id="sub_type' + v_dic + '">' + mark_dicv + '</option>'
                );
            }
        }

    })
    //ajax start 生成年级列表
    $.ajax({
        url: baseURL +'/auth/api/dic/searchDic',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            dicgroup: '年级',
        },
        success: function (e) {
            //for i 循环 start
            for (let i = 0; i < e.content.length; i++) {
                let v_dic = e.content[i].dickey
                let mark_dicv = e.content[i].dicvalue
                $("#grade").prepend(
                    '<option value=' + v_dic + ' mark=' + mark_dicv + ' id="grade_type' + v_dic + '">' + mark_dicv + '</option>'
                );
            }
        }

    })
    //获取必要值
    //学科 默认为0
    subject_val = 0
    //学科更改事件
    $(".add_title_inf").on("change", '#subject', function () {
        subject_val = $(this).val();
    });
    //年级 默认为1
    grade_val = 1
    //年级更改事件
    $(".add_title_inf").on("change", '#grade', function () {
        grade_val = $(this).val();
        console.log(grade_val);
    });
    // ajax start生成指定科目输入框
    $(".add_title_inf").on('change', function () {
        //if 判断 start 如果是单科的
        if (subject_val != 0) {
            $.ajax({
                url: baseURL +'/admin/api/btquestion/searchQuestionCount',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    subject: subject_val,
                    grade: grade_val,
                },
                success: function (e) {
                    $("#all_sub").empty();
                    $("#one_sub").empty();
                    //for i 循环 start
                    for (let i = 0; i < e.content.length; i++) {
                        let def_num = e.content[i].difficulty
                        let def_name = find_arr(arr_onesub_dif, 'difficulty', i + 1, 'name')
                        let def_type = find_arr(arr_onesub_dif, 'difficulty', i + 1, 'type')
                        let sub_type = find_arr(arr_subject, 'sub_val', subject_val, 'type')
                        let sql_num = e.content[i].number
                        $("#one_sub").prepend(
                            '<div class="col-sm-12"><div class="form-group col-sm-3"><label class="col-sm-8 control-label">' + def_name + '单选题个数</label><div class="col-sm-4"><input id="' + def_type + '_' + sub_type + '_num" type="text" class="form-control" placeholder="" value="0"></div></div>'
                            + '<div class="form-group col-sm-3"><label class="col-sm-8 control-label">题库题目个数</label><div class="col-sm-4"><input id="sql_' + def_type + '_' + sub_type + '_num" type="text" class="form-control"  value=' + sql_num + ' readonly></div></div>' +
                            '<div class="form-group col-sm-3"><label class="col-sm-8 control-label">每题分数</label><div class="col-sm-4"><input id="' + def_type + '_' + sub_type + '_fraction" type="text" class="form-control"  value="0"></div></div></div>'
                        );
                    }
                }

            })

        }
        //全科目 ==! 会写成方法  写在外部
        else if (subject_val == 0) {
            all_sub(4)
        }
    })
    //第一次加载页面调用一次全科目方法 frequency=循环次数=科目个数
    all_sub(4)
    //全科目方法
    function all_sub(frequency) {
        $("#all_sub").empty();
        $("#one_sub").empty();
        //ajax start
        //for i 循环 start
        for (let all_sub_i = 0; all_sub_i < frequency; all_sub_i++) {
            $.ajax({
                url: baseURL +'/admin/api/btquestion/searchQuestionCount',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    subject: all_sub_i + 1,
                    grade: grade_val,
                },
                success: function (e) {
                    let sub_name = find_arr(arr_subject, 'sub_val', all_sub_i + 1, 'name')
                    for (let i = 0; i < e.content.length; i++) {
                        let def_num = e.content[i].difficulty
                        let def_name = find_arr(arr_onesub_dif, 'difficulty', i + 1, 'name')
                        let def_type = find_arr(arr_onesub_dif, 'difficulty', i + 1, 'type')
                        let sub_type = find_arr(arr_subject, 'sub_val', all_sub_i + 1, 'type')
                        let sub_name = find_arr(arr_subject, 'sub_val', all_sub_i + 1, 'name')
                        let sql_num = e.content[i].number
                        $("#one_sub").prepend(
                            '<div class="col-sm-12"><div class="form-group col-sm-3"><label class="col-sm-8 control-label">' + sub_name + def_name + '个数</label><div class="col-sm-4"><input id="' + def_type + '_' + sub_type + '_num" type="text" class="form-control" placeholder="" value="0"></div></div>'
                            + '<div class="form-group col-sm-3"><label class="col-sm-8 control-label">题库题目个数</label><div class="col-sm-4"><input id="sql_' + def_type + '_' + sub_type + '_num" type="text" class="form-control"  value="' + sql_num + '" readonly></div></div>' +
                            '<div class="form-group col-sm-3"><label class="col-sm-8 control-label">每题分数</label><div class="col-sm-4"><input id="' + def_type + '_' + sub_type + '_fraction" type="text" class="form-control"  value="0"></div></div></div>'
                        );
                    }
                }

            })
        }


    }

//检测用户操作 并赋值
    $('body').on('change', 'input', function () {

        hig_chi_num = $("#hig_chi_num").val();
        med_chi_num = $("#med_chi_num").val();
        low_chi_num = $("#low_chi_num").val();
        hig_chi_fraction = $("#hig_chi_fraction").val();
        med_chi_fraction = $("#med_chi_fraction").val();
        low_chi_fraction = $("#low_chi_fraction").val();
        C_all_ch = (Math.ceil(low_chi_num) * Math.ceil(low_chi_fraction)) + (Math.ceil(med_chi_num) * Math.ceil(med_chi_fraction)) + (Math.ceil(hig_chi_num) * Math.ceil(hig_chi_fraction))

        hig_math_num = $("#hig_math_num").val();
        med_math_num = $("#med_math_num").val();
        low_math_num = $("#low_math_num").val();
        hig_math_fraction = $("#hig_math_fraction").val();
        med_math_fraction = $("#med_math_fraction").val();
        low_math_fraction = $("#low_math_fraction").val();
        M_all_ch = (Math.ceil(low_math_num) * Math.ceil(low_math_fraction)) + (Math.ceil(med_math_num) * Math.ceil(med_math_fraction)) + (Math.ceil(hig_math_num) * Math.ceil(hig_math_fraction))

        hig_eng_num = $("#hig_eng_num").val();
        med_eng_num = $("#med_eng_num").val();
        low_eng_num = $("#low_eng_num").val();
        hig_eng_fraction = $("#hig_eng_fraction").val();
        med_eng_fraction = $("#med_eng_fraction").val();
        low_eng_fraction = $("#low_eng_fraction").val();
        E_all_ch = (Math.ceil(low_eng_num) * Math.ceil(low_eng_fraction)) + (Math.ceil(med_eng_num) * Math.ceil(med_eng_fraction)) + (Math.ceil(hig_eng_num) * Math.ceil(hig_eng_fraction))

        hig_pol_num = $("#hig_pol_num").val();
        med_pol_num = $("#med_pol_num").val();
        low_pol_num = $("#low_pol_num").val();
        hig_pol_fraction = $("#hig_pol_fraction").val();
        med_pol_fraction = $("#med_pol_fraction").val();
        low_pol_fraction = $("#low_pol_fraction").val();
        P_all_ch = (Math.ceil(low_pol_num) * Math.ceil(low_pol_fraction)) + (Math.ceil(med_pol_num) * Math.ceil(med_pol_fraction)) + (Math.ceil(hig_pol_num) * Math.ceil(hig_pol_fraction))
        //总分
        all_fraction = Math.ceil(P_all_ch + E_all_ch + M_all_ch + C_all_ch)
        //if 判断 start
        if (subject_val == 0) {
            $("#all_fraction").val(all_fraction);
        } else if (subject_val == 1) {
            $("#all_fraction").val(C_all_ch);
        } else if (subject_val == 2) {
            $("#all_fraction").val(M_all_ch);
        } else if (subject_val == 3) {
            $("#all_fraction").val(E_all_ch);
        } else if (subject_val == 4) {
            $("#all_fraction").val(P_all_ch);
        }

    });
    //数量判断赋值
    $('body').on('change', 'input', function () {
        //如果用户修改的是分值 ID不一致 不会生效
        let click_name_id = $(this).attr('id')
        let other_name_id = "sql_" + $(this).attr('id')
        let click_name_val = $("#" + click_name_id).val();
        let other_name_val = $("#" + other_name_id).val();
        $("#" + click_name_id).val(judge(Math.ceil(click_name_val), Math.ceil(other_name_val)))
    });

    //数量判断部分
    function judge(val1, val2) {
        //if 判断 start
        if (val1 > val2) {
            return val2
        } else if (val1 <= val2) {
            return val1
        }
    }


//生成试卷部分
    $("#generate_new_p").click(function () {
        let exam_title = $("#examp_title").val();
        let time = $("#datetime").val() + ":00";
        let exam_time = $("#exam_time").val();
        console.log(exam_title);
        //if 判断 必填项是否输入
        if (exam_time == "") {
            $("#exam_time_div").addClass("has-error");
            alert("未填写考试时长")
            return
        } else if (time == ":00") {
            $("#time_control").addClass("has-error");
            alert("未选择开考时间")
            return
        } else if (exam_title == "") {
            $("#title_div").addClass("has-error");
            alert("未填写试卷标题")
            return
        } else if ($("#all_fraction").val() == 0) {
            alert("未设置题目数量或分数")
            return
        }
        //if 判断科目 start
        if (subject_val == 1) {
            console.log("语文");
            let chi_arr3 = subject_val + "|" + "3|" + hig_chi_num + "|" + hig_chi_fraction
            let chi_arr2 = subject_val + "|" + "2|" + med_chi_num + "|" + med_chi_fraction
            let chi_arr1 = subject_val + "|" + "1|" + low_chi_num + "|" + low_chi_fraction
            let ffsz = chi_arr1 + "," + chi_arr2 + "," + chi_arr3
            console.log(ffsz);
            $.ajax({
                url: baseURL +'/admin/api/btpapers/addBtPapers',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    title: exam_title,
                    type: 1,
                    grade: grade_val,
                    subject: subject_val,
                    ffsz: ffsz,
                    startTime: time,
                    timeLength: exam_time,
                    score: C_all_ch,
                },
                success: function (e) {
                    console.log(e);
                    //if 判断 start
                    if (e.code == -1004) {
                        alert("存在必填项未填写,或服务器错误")
                    } else if (e.code == 1) {
                        alert("上传成功")
                    }
                }

            })

        } else if (subject_val == 2) {

            console.log("数学");
            console.log(subject_val);
        } else if (subject_val == 3) {

            console.log("英语");
            console.log(subject_val);
        } else if (subject_val == 4) {

            console.log("政治");
            console.log(subject_val);
        } else if (subject_val == 0) {
            console.log("摸了");
            console.log(subject_val);

        }

    });

//学科表 暂用
    arr_subject = [{
        sub_val: '0',
        name: '全学科',
        type: 'all',
    }, {
        sub_val: '1',
        name: '语文',
        type: 'chi',
    }, {
        sub_val: '2',
        name: '数学',
        type: 'math',
    }, {
        sub_val: '3',
        name: '英语',
        type: 'eng',
    }, {
        sub_val: '4',
        name: '政治',
        type: 'pol',
    }]
//难度表 暂用
    arr_onesub_dif = [{
        difficulty: 1,
        name: "低难度单选",
        type: 'low'
    }, {
        difficulty: 2,
        name: "中难度单选",
        type: 'med'

    }, {
        difficulty: 3,
        name: "高难度单选",
        type: 'hig'

    }]

})

//find_arr方法 arr目标数组 key等于val时 返回target_key
function find_arr(arr, key, val, target_key) {
    //for i 循环 start
    for (let i = 0; i < arr.length; i++) {
        //if 判断 start
        if (arr[i][key] == val) {
            return arr[i][target_key]
        }
    }
    return -1
}
